var rootPath = getRootPath();
var oriPhone;
var oriEmail;

$(function() {
	
	var param = "";
	var callbackUrl = getUrlParam("callbackUrl");//getUrlParam 带转义
	if(callbackUrl){
		param += "&callbackUrl=" +  encodeURIComponent(callbackUrl);
	}else{
		param += "";
	}
	//由于此认证是跳转进入认证，因此 加入uid验证
	var uid = getUrlParam("uid");
	if(uid == undefined || uid ==''){
		layer.alert("认证链接错误，请重试");
		return;
	}else{
		$("#uid").val(uid);
		param += "&uid=" +  encodeURIComponent(uid);
	}
	
	
	//滚动条置顶
	$("body").prepend('<input type="text" id="focus" size="1">');
    $("#focus").focus();
    $("#focus").hide();
    
 	var rleId = $("#rleId").val();
	var prevPage = getUrlParam("currentPage");
	
	$(".city").css("cursor", "pointer");
	//回显
	$.ajax({
		url: rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type: 'GET',
		success: function(data) {
			if(data.returnCode == '0000') {
				var obj = data.returnObject;
			
//				$("#role").find("option[value='" + obj.sort + "']").prop("selected","selected");
//				var sortVal = $("#role").val();
//				$("#sort").val(sortVal);
				oriPhone = obj.phone;
				$("#phone").val(obj.phone);
				oriEmail = obj.email;
				$("#email").val(obj.email);
				$("#job").find("option[value='" + obj.job + "']").prop("selected","selected");
				$("#school").html("<option value='" + obj.school.sid + "'>" + obj.school.sname + "</option>");
			}
		}
	});
	
	//上一步
	$("#prev").click(function() {
		//4为原来的教练计划数字
		parent.$("#content").prop("src", prevPage + "?rleId=htc&creType=16&title=校长训练营认证" + param);
	});
	
	//申请认证
	$("#applyAuth").click(function() {
		if(!checkData()){
			return;
		}
		 
		showLoading();
		$("#htcAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功",{icon:1,time:1000},function() {	
						if(callbackUrl){
							parent.document.location.href = callbackUrl;
						} else {
							parent.$("#content").prop("src","applyResult.html?rleId=htc&title=校长训练营认证");
						}
					});
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("申请失败,请稍后重试");
			}
		});
		return false;
	});
});

//根据不同 sort验证
function checkData(){
	var sort = $("#sort").val(); 
	
	if($.trim($("#uid").val()) == '') {
		layer.alert("认证链接错误，请重试!");
		return false;
	}
	
	if($.trim($("#role").val()) == '') {
		$("#role").focus();
		showErrMsg("role", "请选择角色");
		return false;
	}
	
	var sid=$("#school option:selected").val();
	if($.trim(sid) == '') {
		$("#school").focus();
		showErrMsg("school","请选择学校");
		return false;
	}else{
		hideMsg("school");
	}
	if($.trim($("#job").val()) == '') {
		$("#job").focus();
		showErrMsg("job","请选择职务");
		return false;
	}
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone", "请输入手机号码");
		return false;
	} else {
		if($("#phone").val() != oriPhone
				&& !checkPhone($("#phone").val())) {
			$("#phone").focus();
			showErrMsg("phone","手机号码格式有误");
			return false;
		}
	}
	
	if($.trim($("#email").val()) == '') {
		if(sort && sort !=7){
			$("#email").focus();
			showErrMsg("email","请输入邮箱地址");
			return false;
		}
	} else {
		if($("#email").val() != oriEmail
				&&!checkEmail($("#email").val())) {
			$("#email").focus();
			showErrMsg("email","邮箱格式有误");
			return false;
		}
	}
	 
	return true;
}